<?php
/**
 * Created by PhpStorm.
 * User: wuxiang
 * Date: 2016/8/23
 * Time: 11:38
 */

namespace Scratch\Controller;

use Common\Controller\AdminbaseController;
use Common\Libiary\BaseApiHttp;
use Common\Libiary\Uploadify;

class ScratchController extends AdminbaseController
{
    public $url = '';
    public $data = [];
    public $arr = [];
    public $res = [];
    public $arrres = [];
    public $page = 1;
    public $type;
    public $list;
    public $demain = TP_DEBUG ?  "http://m.9now.net/" : "http://m.mwee.cn/";

    //导入excel
    function index(){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();

        $this->url = $this->demain . 'api/operate/grab/scrathindex';

        $ret = $baseApi->https($this->url, 'POST', $this->data);
        $this->list = json_decode($ret, true);
        $this->assign('list',$this->list);

        $this->display();
    }


    function addExcel(){

        Vendor('Classes.PHPExcel');
        $tmp_file = $_FILES ['file_stu'] ['tmp_name'];

        $file_types = explode ( ".", $_FILES ['file_stu'] ['name'] );
        $file_type = $file_types [count ( $file_types ) - 1];

        /*判别是不是.xls文件，判别是不是excel文件*/
        if (strtolower ( $file_type ) != "xlsx" && strtolower ( $file_type ) != "xls")
        {
            $this->error ( '不是Excel文件，重新上传' );
        }


        /*设置上传路径*/
        $savePath = './public/upload/';
        /*以时间来命名上传的文件*/
        $str = date ( 'Ymdhis' );
        $file_name = $str . "." . $file_type;

        /*是否上传成功*/
        if (!copy ( $tmp_file, $savePath . $file_name ))
        {
            $this->error ( '上传失败' );
        }


        $file = $savePath.$file_name;

        $objPHPExcel = \PHPExcel_IOFactory::load($file);
        $arrExcel = $objPHPExcel->getSheet(0)->toArray();

        array_shift($arrExcel);
        array_shift($arrExcel);

        //组合数组

        $arr = [];
        if(count($arrExcel)>0){
            foreach($arrExcel as $k=>$v){
                $arr[$v[0]]['cityid'] = trim($v[0]);
                $arr[$v[0]]['shopName'] = trim($v[1]);
                $arr[$v[0]]['Title'] = trim($v[1]);
                $arr[$v[0]]['ShopID'] = trim($v[2]);
                $arr[$v[0]]['MasterID'] = trim($v[2]);
                $arr[$v[0]]['dateRange'] = explode('-',$v[3]);
                $arr[$v[0]]['PageCouponText'] = explode(';',$v[4]);
                $arr[$v[0]]['Introduction'] = trim($v[5]);
                $arr[$v[0]]['CouponID'][] = trim($v[6]);
                $arr[$v[0]]['CouponTitle'][] = $v[1].$v[8].$v[7];
                $arr[$v[0]]['Extra'][] = trim($v[9]);
                $arr[$v[0]]['Weight'][] = substr($v['10'],0,'-1');
                $arr[$v[0]]['max'] = 100;
                $arr[$v[0]]['BranchList'][] = trim($v[11]);
                $arr[$v[0]]['DateText'] = trim($v[12]);
            }
        }


        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();

        $this->url = $this->demain . 'api/operate/grab/addexcel';

        $res = array_merge($this->data,['info'=>$arr]);

        $ret = $baseApi->https($this->url, 'POST', $res);

        $this->list = json_decode($ret, true);

        if($this->list['data']['activityKey']){
            $this->success('导入成功',U('Scratch/index'));
        }else{

            $this->success('导入失败',U('Scratch/index'));
        }

    }
}

